var $z1PrefabInfo = require("PrefabInfo");
cc.Class({
  extends: cc.Component,
  properties: {
    dragingRoot: cc.Node,
    blockRootNode: cc.Node,
    blockItemsRootNode: cc.Node,
    buffEffect: $z1PrefabInfo,
    barPrefab: $z1PrefabInfo,
    linePrefab: $z1PrefabInfo,
    adItemPrefab: $z1PrefabInfo
  },
  onLoad: function () {
    this.blockRoot = this.blockRootNode.getComponent("BlockRoot");
    this.adItemIds = [];
  },
  layoutChildren: function () {
    var t = this.node.getComponent(cc.Layout);
    t.spacingX = -1;
    if (this.node.childrenCount > 1) {
      var e = this.node.children.reduce(function (t, e) {
        return t + e.width;
      }, 0);
      if (e > 720) {
        var i = Math.max(.8, 720 / e);
        this.node.children.forEach(function (t) {
          return t.scale = i;
        });
        t.spacingX = 0;
      } else {
        t.spacingX = Math.min(30, (720 - e) / (this.node.childrenCount - 1));
      }
    } else {
      t.spacingX = 15;
    }
    t.updateLayout();
  },
  setCanDrag: function (t) {
    this.node.children.forEach(function (e) {
      var i = e.getComponent("Item");
      i && 0 != i.id && i.setCanDrag(t);
    });
    this.blockItemsRootNode.children.forEach(function (e) {
      var i = e.getComponent("Item");
      i && 0 != i.id && i.setCanDrag(t);
    });
  },
  pickupFromBoard: function (t, e) {
    var i = this.dragingRoot;
    var n = i.convertToNodeSpaceAR(e);
    t.node.position = n;
    t.node.angle = 0;
    t.node.parent = i;
    cc.Tween.stopAllByTarget(t.node);
    t.initSameIDLvLine();
  },
  pickup: function (t, e) {
    var i = this.dragingRoot;
    var n = i.convertToNodeSpaceAR(e);
    t.node.position = n;
    t.node.angle = 0;
    t.node.parent = i;
    t.node.scale = 1;
    cc.Tween.stopAllByTarget(t.node);
    var o = 0 == t.id;
    this.blockRoot.setExtendMode(o);
    o && this.setCanDrag(false);
  },
  putdown: function (t, e) {
    var i = this.node.convertToNodeSpaceAR(e);
    t.setBgSpParent(true);
    if (t.canUse && 0 != t.id && t.lv < 3 && i.y < 100) {
      for (var n = 0; n < this.node.childrenCount; n++) {
        var o = this.node.children[n];
        if (i.x > o.x - o.width / 2 && i.x < o.x + o.width / 2) {
          var s = o.getComponent("Item");
          if (s.canUse && s.id == t.id && s.lv == t.lv) {
            t.node.parent = null;
            this.layoutChildren();
            return void s.lvup();
          }
        }
      }
    }
    t.node.parent = this.node;
    t.node.position = i;
    t.setSelfListenEvent(true);
    var c = this.node.children.findIndex(function (e) {
      return e.x > t.node.x;
    });
    c = -1 == c ? this.node.childrenCount : c;
    for (var a = 0; a < c; a++) {
      this.node.children[a].zIndex = -1;
    }
    for (var r = c; r < this.node.childrenCount; r++) {
      this.node.children[r].zIndex = 1;
    }
    t.node.zIndex = 0;
    this.node.sortAllChildren();
    this.layoutChildren();
    cc.tween(t.node).to(.16, {
      y: 0
    }).start();
  },
  showAdItem: function (t) {
    if (!this.adItemIds.some(function (e) {
      return e == t.id;
    })) {
      this.adItemIds.push(t.id);
      var e = this.adItemPrefab.addNode();
      var i = e.getComponent(cc.Animation);
      i.play("adhero-in");
      i.once("finished", function () {
        i.play("adhero-stand");
      });
      var n = cc.instantiate(t.spine.node);
      n.scale = this.scene.heroJsonFile.json[t.id - 1].scale;
      n.position = cc.Vec2.ZERO;
      n.parent = cc.find("hero", e);
      e.getComponent(cc.Button).clickEvents[0].customEventData = t.id;
      e.____lineIKBones = [];
    }
  },
  onClickAdItem: function (t, e) {
    var i = this;
    var n = parseInt(e);
    cc.pvz.AdUtils.showAdRewardVideo(1, function (e) {
      if (e) {
        i.scene.newEquipItem(2, n);
        cc.find("hero", t.target).children[0].destroy();
        t.target.____lineIKBones.forEach(function (t) {
          return t.spine.node.parent = null;
        });
        t.target.____lineIKBones.length = 0;
        t.target.parent = null;
        var o = i.adItemIds.findIndex(function (t) {
          return t == n;
        });
        i.adItemIds.splice(o, 1);
        i.scene.saveRuntimeData();
      }
    });
  },
  clearAllAdItems: function () {
    this.adItemPrefab.root.children.forEach(function (t) {
      cc.find("hero", t).children[0].destroy();
      t.____lineIKBones.forEach(function (t) {
        return t.spine.node.parent = null;
      });
      t.____lineIKBones.length = 0;
    });
    this.adItemPrefab.root.removeAllChildren();
    this.adItemIds.length = 0;
  },
  update: function () {
    var t = this;
    this.adItemPrefab.root.children.forEach(function (e, i) {
      var n = t.adItemIds[i];
      var o = [];
      var s = function (t) {
        var e = t.getComponent("Item");
        e.canUse && e.id == n && 2 == e.lv && o.push(e);
      };
      t.blockItemsRootNode.children.forEach(function (t) {
        return s(t);
      });
      t.node.children.forEach(function (t) {
        return s(t);
      });
      for (var c = e.____lineIKBones.length; c < o.length; c++) {
        var a = t.linePrefab.addNodeTo(e, cc.Vec2.ZERO);
        a.zIndex = cc.macro.MAX_ZINDEX;
        var r = a.getComponent(sp.Skeleton);
        var h = r.findBone("IK");
        e.____lineIKBones.push({
          spine: r,
          IKBone: h
        });
      }
      for (var d = 0; d < o.length; d++) {
        var u = e.____lineIKBones[d];
        var l = u.spine.node.convertToNodeSpaceAR(o[d].spine.node.convertToWorldSpaceAR(cc.v2(0, 45)));
        u.IKBone.x = l.x;
        u.IKBone.y = l.y;
      }
      for (var p = o.length; p < e.____lineIKBones.length; p++) {
        e.____lineIKBones[p].spine.node.parent = null;
      }
      e.____lineIKBones.length = o.length;
    });
  }
});